# Maintainer: Sven-Hendrik Haase <svenstaro@archlinux.org>
# Contributor: bartus <arch-user-repoᘓbartus.33mail.com>

pkgname=meshroom
pkgver=2021.1.0
pkgrel=10
pkgdesc="A free, open-source 3D Reconstruction Software based on the AliceVision framework"
arch=('loong64' 'x86_64')
url="https://alicevision.github.io/"
license=('MPL2')
depends=('alice-vision' 'alembic' 'openimageio' 'python-psutil' 'popsift'
         'pyside2' 'qt5-quickcontrols' 'qt5-quickcontrols2' 'qt5-3d' 'qt5-graphicaleffects'
         'qt5-imageformats' 'qt5-location' 'qt5-svg' 'qt5-charts')
makedepends=('git' 'cmake' 'python-idna' 'python-setuptools' 'ninja' 'boost' 'coin-or-lemon')
optdepends=('alice-vision-cuda: for DepthMap nodes')
source=("${pkgname}::git+https://github.com/alicevision/meshroom.git#tag=v${pkgver}"
        "git+https://github.com/alicevision/QtOIIO.git#tag=v${pkgver}"
        "git+https://github.com/alicevision/qmlAlembic.git#tag=v${pkgver}"
        "voctree::git+https://gitlab.com/alicevision/trainedVocabularyTreeData.git"
        "git+https://github.com/alicevision/qtAliceVision.git#commit=517fc5ffb43471b12f71aa8b9881cf0634647a93^"
        "python-compat.patch"
        "pyside_property_error.patch")
sha256sums=('SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            '8f326257d7b76136a600ce7ae5785337f98725d5b54f7f44320d60f6a59244a8'
            'e211783ead22d388c72f60bac7ab95d670a4d6ae196225c15038b5c9e7c80fdc')

prepare() {
  cd meshroom

  # Hardcode camera_database and voctree default value
  sed -i "s:'ALICEVISION_VOCTREE', '':'ALICEVISION_VOCTREE', '/usr/share/${pkgname}/vlfeat_K80L3.SIFT.tree':g" meshroom/nodes/aliceVision/*.py
  sed -i "s:'ALICEVISION_SENSOR_DB', '':'ALICEVISION_SENSOR_DB', '/usr/share/aliceVision/cameraSensors.db':g" meshroom/nodes/aliceVision/*.py

  # Fix for QtOIIO plugin to build against openexr:3
  sed -i '1 i\#include <cmath>' "${srcdir}"/QtOIIO/src/jetColorMap.hpp
  sed -i 's|imageformats|plugins/imageformats|' "${srcdir}"/QtOIIO/src/imageIOHandler/CMakeLists.txt

  sed -i 's|${OPENIMAGEIO_LIBRARIES}|OpenImageIO::OpenImageIO|g' "${srcdir}"/QtOIIO/src/{imageIOHandler,depthMapEntity}/CMakeLists.txt
  sed -i 's|${OPENIMAGEIO_INCLUDE_DIRS}|${OpenImageIO_INCLUDE_DIRS}|g' "${srcdir}"/QtOIIO/src/{imageIOHandler,depthMapEntity}/CMakeLists.txt
  sed -i 's|OPENIMAGEIO_FOUND|OpenImageIO_FOUND|' "${srcdir}"/QtOIIO/CMakeLists.txt
  rm -rf "${srcdir}"/QtOIIO/cmake/

  # Fix for qmlAlembic plugin to build against openexr:3
  sed -i '/find_package(IlmBase REQUIRED)/d' "${srcdir}"/qmlAlembic/CMakeLists.txt
  sed -i 's|${ILMBASE_INCLUDE_DIR}||' "${srcdir}"/qmlAlembic/src/CMakeLists.txt
  rm -rf "${srcdir}"/qmlAlembic/cmake/

  # avoid bug in pyside 5.15.2 (https://bugreports.qt.io/browse/PYSIDE-1426)
  patch -Np1 -i "$srcdir"/pyside_property_error.patch

  patch -Np1 -i "$srcdir"/python-compat.patch
}

build() {
  cd "${srcdir}"/QtOIIO
  cmake \
    -Bbuild \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="/usr/lib/qt" \
    -DCMAKE_BUILD_TYPE=None
  ninja -C build

  cd "${srcdir}"/qmlAlembic
  cmake \
    -Bbuild \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="/usr/lib/qt" \
    -DCMAKE_BUILD_TYPE=None
  ninja -C build

  cd "${srcdir}"/qtAliceVision
  cmake \
    -Bbuild \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="/usr/lib/qt" \
    -DCMAKE_BUILD_TYPE=None
  ninja -C build
}

package() {
  DESTDIR="${pkgdir}" ninja -C QtOIIO/build install
  DESTDIR="${pkgdir}" ninja -C qmlAlembic/build install
  DESTDIR="${pkgdir}" ninja -C qtAliceVision/build install

  cd meshroom
  install -Dm755 meshroom/ui/__main__.py "${pkgdir}"/usr/bin/meshroom
  install -m755 -t "${pkgdir}"/usr/bin bin/meshroom_*

  local python_version=$(python -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')
  mkdir -p "${pkgdir}"/usr/lib/python"${python_version}"
  cp -r meshroom "${pkgdir}"/usr/lib/python"${python_version}"

  install -Dm644 -t "${pkgdir}"/usr/share/meshroom "${srcdir}"/voctree/vlfeat_K80L3.SIFT.tree
}
# vim:set ts=2 sw=2 et:
